from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin
from rest_framework.response import Response
from apps.minapp import models
from rest_framework import serializers
from time import strftime
from random import Random
from django_filters.rest_framework import FilterSet, ChoiceFilter, DjangoFilterBackend
from rest_framework.pagination import PageNumberPagination
import django.utils.timezone as timezone
import datetime, time
from django.db.models import Sum, F
from django.db import models as dbmodel



class S_NoticeSerializer(serializers.ModelSerializer):
    # publishTime = serializers.DateTimeField(read_only=True)
    publishTime = serializers.SerializerMethodField()
    windowTitle = serializers.SerializerMethodField()
    windowIcon = serializers.SerializerMethodField()
    image = serializers.SerializerMethodField()

    @staticmethod
    def get_windowTitle(obj):
        return obj.window.title

    @staticmethod
    def get_publishTime(obj):
        return str(obj.publishTime)[:19]

    @staticmethod
    def get_image(obj):
        images = obj.image.all().values_list('src', flat=True)
        # print(images)
        return images

    @staticmethod
    def get_windowIcon(obj):
        return str(obj.window.icon)

    class Meta:
        model = models.S_Notice
        fields = '__all__'


class C_NoticeSerializer(serializers.ModelSerializer):
    # publishTime = serializers.DateTimeField(read_only=True)
    publishTime = serializers.SerializerMethodField()

    @staticmethod
    def get_publishTime(obj):
        return str(obj.publishTime)[:19]

    class Meta:
        model = models.C_Notice
        fields = '__all__'


class S_NoticeDisplaySerializer(serializers.ModelSerializer):
    publishTime = serializers.SerializerMethodField()
    windowTitle = serializers.SerializerMethodField()

    @staticmethod
    def get_publishTime(obj):
        return str(obj.publishTime)[:19]

    @staticmethod
    def get_windowTitle(obj):
        return obj.window.title

    class Meta:
        model = models.S_Notice
        fields = ['id', 'title', 'content', 'publishTime', 'windowTitle']


class C_NoticeDisplaySerializer(serializers.ModelSerializer):
    publishTime = serializers.SerializerMethodField()

    @staticmethod
    def get_publishTime(obj):
        return str(obj.publishTime)[:19]

    class Meta:
        model = models.C_Notice
        fields = ['id', 'title', 'content', 'publishTime']


class S_NoticeView(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, CreateModelMixin, GenericViewSet):
    queryset = models.S_Notice.objects.all()
    serializer_class = S_NoticeSerializer

    def create(self, request, *arsg, **kwargs):
        print(self.request.data)

        notice = models.S_Notice()
        # 获取此时时间
        notice.publishTime = timezone.now()
        # 设置保存时间
        # holdTime = self.request.data['holdTime'] + notice.publishTime

        notice.title = self.request.data['title']
        notice.content = self.request.data['content']
        # notice.holdTime = holdTime
        notice.status = self.request.data['status']
        notice.staff_id = 1
        notice.window_id = 1
        notice.save()
        return Response("ok")

    def list(self, request, *args, **kwargs):
        result = []
        queryset = models.S_Notice.objects.filter(status__in=[2]).all().order_by('status')
        for notice in queryset:
            result.append(S_NoticeDisplaySerializer(notice).data)
        # print(result)
        return Response(result)

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        print(instance.id)
        if instance.id == 0:  # 从创建公告页面发送回来，需要寻找有没有保存但未发布的数据
            notice = self.get_queryset().get(status="1")
        else:  # 从修改公告页面发送回来，寻找需要修改的数据
            notice = self.get_queryset().get(id=instance)
        return Response(S_NoticeSerializer(notice).data)

    def update(self, request, *args, **kwargs):
        print(self.request.data)
        id = kwargs['pk']
        notice = models.S_Notice.objects.get(id=id)
        notice.title = self.request.data['title']
        notice.content = self.request.data['content']
        notice.status = self.request.data['status']
        notice.save()
        return Response("ok")


class C_NoticeView(ListModelMixin, CreateModelMixin, GenericViewSet):
    queryset = models.C_Notice.objects.all()
    serializer_class = C_NoticeSerializer

    def list(self, request, *args, **kwargs):
        result = []
        queryset = models.C_Notice.objects.filter(status__in=[2]).all().order_by('status')
        for notice in queryset:
            result.append(C_NoticeDisplaySerializer(notice).data)
        # print(result)
        return Response(result)


class NoticeDetailView(RetrieveModelMixin, GenericViewSet):
    def retrieve(self, request, *args, **kwargs):
        id = kwargs['pk']
        print(self.request.query_params)
        mode = self.request.query_params['mode']

        if mode == '0':  # 餐厅公告
            instance = models.C_Notice.objects.filter(id=id).first()
            return Response(C_NoticeSerializer(instance).data)
        else:  # 窗口公告
            instance = models.S_Notice.objects.filter(id=id).first()
            return Response(S_NoticeSerializer(instance).data)
